/**
 * 
 */
package neptune.web.client.view.components.editor;

import neptune.web.client.model.topology.SerializableIPAddress;
import neptune.web.client.view.input.IPInputValidator;

import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.CheckBox;
import com.google.gwt.user.client.ui.ClickListener;
import com.google.gwt.user.client.ui.DialogBox;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.ListBox;
import com.google.gwt.user.client.ui.ScrollPanel;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.TreeItem;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;

/**
 * @author Roberto Bifulco [RobertoBifulco.it] [info@robertoBifulco.it]
 * 
 */
public class IpAddrPropsDialog extends DialogBox {

	private TreeItem treeItem;
	private FlexTable mainPanel;
	private TextBox address;
	private TextBox subnet;
	private ListBox version;
	private CheckBox control;
	private ClickListener okListener = new ClickListener() {
		public void onClick(Widget sender) {
			if (validateInputs()) {
				serAddr.setAddress(address.getText());
				serAddr.setSubnetMask(subnet.getText());
				serAddr.setVersion(Integer.parseInt(version.getItemText(version
						.getSelectedIndex())));
				serAddr.setControlAddress(control.isChecked());

				IpAddressMenuBar widget = (IpAddressMenuBar) treeItem
						.getWidget();
				widget.getRootItem().setText(serAddr.getAddress());
				IpAddrPropsDialog.this.hide();
			}
		}
	};
	private ClickListener cancelListener = new ClickListener() {
		public void onClick(Widget sender) {
			if (cancelCallBack != null)
				cancelCallBack.onCancel();
			IpAddrPropsDialog.this.hide();
		}
	};
	private SerializableIPAddress serAddr;
	private CancelCallBack cancelCallBack;

	public IpAddrPropsDialog(SerializableIPAddress addr, TreeItem treeItem) {

		super(false, true);

		this.treeItem = treeItem;

		mainPanel = new FlexTable();
		mainPanel.setWidth("95%");
		ScrollPanel mainPanelWrapper = new ScrollPanel(mainPanel);
		mainPanelWrapper.setHeight("250px");

		/*
		 * Inizio costruzione campi per inserimemnto dati
		 */
		// ADDR
		Label addressLabel = new Label("Ip address");
		addressLabel.setStylePrimaryName("NodePropsDialog-label");
		address = new TextBox();
		address.setStylePrimaryName("NodePropsDialog-input");
		mainPanel.setWidget(0, 0, addressLabel);
		mainPanel.setWidget(0, 1, address);

		// SUBNET
		Label subnetLabel = new Label("Subnet mask");
		subnetLabel.setStylePrimaryName("NodePropsDialog-label");
		subnet = new TextBox();
		subnet.setStylePrimaryName("NodePropsDialog-input");
		mainPanel.setWidget(1, 0, subnetLabel);
		mainPanel.setWidget(1, 1, subnet);

		// VERSION
		Label versionLabel = new Label("Ip version");
		versionLabel.setStylePrimaryName("NodePropsDialog-label");
		version = new ListBox();
		version.setStylePrimaryName("NodePropsDialog-input");
		version.addItem("4");
		version.addItem("6");
		version.setSelectedIndex(0);
		mainPanel.setWidget(2, 0, versionLabel);
		mainPanel.setWidget(2, 1, version);

		// CONTROL
		Label controlLabel = new Label("Control address");
		controlLabel.setStylePrimaryName("NodePropsDialog-label");
		control = new CheckBox();
		control.setStylePrimaryName("NodePropsDialog-input");
		control.setChecked(false);
		// mainPanel.setWidget(3, 0, controlLabel);
		// mainPanel.setWidget(3, 1, control);

		// HELP
		HTML help = new HTML("Neptune provides an automatic "
				+ "IP address generation system to realize "
				+ "the topology.<br/><br/>"
				+ "Users should not define IP addresses "
				+ "on network interfaces. The specification of an IP "
				+ "address through this interface is under "
				+ "user's responsibility, no guarantees are "
				+ "given on the correct realization of the topology.");
		help.setStylePrimaryName("IPPropsDialog-help");
		mainPanel.setWidget(3, 0, help);
		mainPanel.getFlexCellFormatter().setColSpan(3, 0, 2);

		// BUTTON PANEL
		HorizontalPanel buttonPanel = new HorizontalPanel();
		Button ok = new Button("Ok");
		ok.addClickListener(okListener);
		ok.setStylePrimaryName("NodePropsDialog-button");
		Button cancel = new Button("Cancel");
		cancel.addClickListener(cancelListener);
		cancel.setStylePrimaryName("NodePropsDialog-button");
		buttonPanel.add(ok);
		buttonPanel.add(cancel);

		VerticalPanel vPanel = new VerticalPanel();
		vPanel.setWidth("100%");
		vPanel.add(mainPanelWrapper);
		vPanel.add(buttonPanel);
		vPanel.setCellHorizontalAlignment(buttonPanel,
				VerticalPanel.ALIGN_RIGHT);

		this.setWidget(vPanel);
		this.setText("Ip Address Properties");

		// Styles
		mainPanelWrapper.setStylePrimaryName("NodePropsDialog-mainWrapper");
		mainPanel.setStylePrimaryName("NodePropsDialog-mainPanel");
		this.setStylePrimaryName("NodePropsDialog");

		setData(addr);
	}

	public boolean validateInputs() {
		// Resetta le segnalazioni d'errore
		address.removeStyleDependentName("error");
		subnet.removeStyleDependentName("error");

		IPInputValidator ipValidator = new IPInputValidator();

		boolean[] errors = new boolean[2];
		for (int i = 0; i < errors.length; i++)
			errors[i] = true;

		if (!ipValidator.isValid(address.getText())) {
			errors[0] = false;
			address.addStyleDependentName("error");
		}
		if (!ipValidator.isValid(subnet.getText())) {
			errors[1] = false;
			subnet.addStyleDependentName("error");
		}

		for (int i = 0; i < errors.length; i++) {
			if (!errors[i])
				return false;
		}

		return true;
	}

	public void setData(SerializableIPAddress addr) {
		address.setText(addr.getAddress());
		subnet.setText(addr.getSubnetMask());
		if (addr.getVersion() == 4)
			version.setSelectedIndex(0);
		else
			version.setSelectedIndex(1);
		control.setChecked(addr.isControlAddress());

		this.serAddr = addr;
	}

	public void setCancelCallback(CancelCallBack cancelCallBack) {
		this.cancelCallBack = cancelCallBack;
	}

	/**
	 * @return the cancelCallBack
	 */
	public CancelCallBack getCancelCallBack() {
		return cancelCallBack;
	}

}
